Identifiers

In programming languages, Identifiers are used for identification purpose. In Scala, an identifier can be a
  • class name
  • method name
  • variable name
  • object name
class Demo{
var a: Int = 20
}
object Main {
def main(args: Array[String]) {
var ob = new Demo();
}
}

In the above program we have 6 idtentifiers:
  • Demo: Class name
  • a: Variable name
  • Main: Object name
  • main: Method name
  • args: Variable name
  • ob: Object name
Note: There are certain rules for defining a valid Scala identifier. These rules must be followed, otherwise we get a compile-time error.
  • Scala identifiers are case-sensitive.
  • Scala does not allows you to use keyword as an identifier.
  • Reserved Words can’t be used as an identifier like $ etc.
  • Scala only allowed those identifiers which are created using below four types of identifiers.
  • There is no limit on the length of the identifier, but it is advisable to use an optimum length of 4 – 15 letters only.
  • Identifiers should not start with digits([0-9]). For example “123variable” is a not a valid Scala identifier.
object demo
{
def main(args: Array[String])
{
var name = "Siya";
var _age = 20;
var Branch = "Computer Science";
println("Name:" +name);
println("Age:" +_age);
println("Branch:" +Branch);
}
}
Output:
Name:Siya
Age:20
Branch:Computer Science

In the above example, valid identifers are:
Main, main, args, `name`, _age, Branch, +and keywords are:
Object, def, var, println
Types of Scala identifiers are
  • Alphanumeric Identifiers 
  • Operator Identifiers
  • Mixed IdentiFiers
  • Literal Identifiers
Alphanumeric Identifiers: its  start with a letter(capital or small letter) or an underscore and followed by letters, digits, or underscores. The $ character is reserved keyword and cannot be used as identifiers.

Example of valid alphanumeric identifiers:
 _stud, emp123, _1_abc_23, Student

Example of Invalid alphanumeric identifiers:
123G, $amount, -total
object demo
{
def main(args: Array[String])
{
var _student5: String = "Ramesh"
var Last_name: String = "Shukla"
println(_student5);
println(Last_name);
}
}
Output:
Ramesh
Shukla

Operator Identifiers: An operator contain one or more operator character like +, :, ?, ~, or # etc.

Example of vaild operator identifiers:
 +, ++

object Main
{
    def main(args: Array[String])
    {
    var x:Int = 20;
    var y:Int = 10;
    var sum = x + y;
    println("Display the result of + identifier:");
    println(sum);
    }
}

Output:

Display the result of + identifier:
30

Mixed Identifiers: It contains alphanumeric identifiers followed by underscore and an operator identifier.

Example of vaild mixed identifiers:
 unary_+, sum_=

object Main
{
    def main(args: Array[String])
    {
    var num_+ = 20;    // num_+ is a valid mixed identifier
    println("Display the result of mixed identifier:");
    println(num_+);
    }
}

Output:
Display the result of mixed identifier:
20

Literal Identifiers: A literal is an  arbitrary string enclosed with back ticks (`….`)
Example of vaild mixed identifiers:
`Student`, `name`

object Main
{
    def main(args: Array[String])
    {
    var `name` = "Siya"
    var `age` = 20
    println("Name:" +`name`);
    println("Age:" +`age`);
    }
}
Output:

Name:Siya
Age:20

No comments:

Post a Comment